//
//  QRCodeViewController.swift
//  sinaWeibo
//
//  Created by 张国梁 on 16/4/6.
//  Copyright © 2016年 CoderGL. All rights reserved.
//

import UIKit
import AVFoundation

class QRCodeViewController: UIViewController {

    @IBOutlet weak var tabBar: UITabBar!
    
    @IBAction func dismiss(sender: UIBarButtonItem) {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    @IBOutlet weak var containerView: UIView!
    
    @IBOutlet weak var scanLineView: UIImageView!
    
    @IBOutlet weak var scanLineViewTopConstraint: NSLayoutConstraint!
    
    @IBOutlet weak var containerHeightConstraint: NSLayoutConstraint!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tabBar.selectedItem = tabBar.items![0]
        
    }
  
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        startAnimation()
        
        scanQRCode()
    }
    
    
    private func startAnimation() {
//        self.scanLineViewTopConstraint.constant = -self.containerHeightConstraint.constant
//        self.scanLineView.layoutIfNeeded()
        
        UIView.animateWithDuration(2.0) {
            self.scanLineViewTopConstraint.constant = self.containerHeightConstraint.constant
            UIView.setAnimationRepeatCount(MAXFLOAT)
            self.scanLineView.layoutIfNeeded()
        }
    }
    
    private func scanQRCode() -> Void {
        // 1.判断能否将输入设备加入到会话中
        if !session.canAddInput(input){
            return
        }
        // 2.判断能否将输出设备添加到会话中
        if !session.canAddOutput(output) {
            return
        }
        // 3.将输出设备和输入设备添加到会话中
        session.addInput(input)
        session.addOutput(output)
        
        // 4.设置输出设备解析数据的类型(先添加输出设备才能设置输出设备的类型)
        output.metadataObjectTypes = output.availableMetadataObjectTypes
        
        // 5.设置输出设备的代理
        output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
        // 5.将预览图层添加到View上进行显示
        view.layer.insertSublayer(previewLayer, atIndex: 0)
        // 6.开始扫描
        session .startRunning()
    }
    
    
    // 会话
    private lazy var session : AVCaptureSession = AVCaptureSession()
    
    // 拿到输入设备
    private lazy var input: AVCaptureDeviceInput? = {
        // 获取摄像头
        let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
        do{
            // 创建输入对象
            let input = try AVCaptureDeviceInput(device: device)
            return input
        }catch
        {
            print(error)
            return nil
        }
    }()
    
    // 输出设备
    
    private lazy var output : AVCaptureMetadataOutput = AVCaptureMetadataOutput()
    
    // 创建预览图层
    private lazy var previewLayer: AVCaptureVideoPreviewLayer = {
        let layer = AVCaptureVideoPreviewLayer(session: self.session)
        layer.frame = UIScreen.mainScreen().bounds
        layer.metadataOutputRectOfInterestForRect(self.containerView.frame)
        return layer
    }()
  
}

extension QRCodeViewController : UITabBarDelegate {
    
    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        if item==tabBar.items![0] {
            containerHeightConstraint.constant = 200
            
        } else {
            containerHeightConstraint.constant = 100
        }
    }
}

extension QRCodeViewController : AVCaptureMetadataOutputObjectsDelegate {
    
    
    func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
        print(metadataObjects);
    }
}
